package com.daveychu.lingo.model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WordList {

	private List<String> words = new ArrayList<String>();

	/**
	 * 
	 * @throws IOException
	 */
	public WordList() throws IOException {
		BufferedReader in = null;
		String word;
		try {
			in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("5letterwords.txt")));
			while ((word = in.readLine()) != null) {
				words.add(word);
			}
		} catch (FileNotFoundException e) {
			Logger.getAnonymousLogger().warning(e.getMessage());
		} finally {
			in.close();
		}
	}

	/**
	 * Returns a random word from the predefined list
	 * 
	 * @return an array of Letter's created from a random word from the list
	 */
	public Letter[] getRandomWord() {
		int random = (int) Math.floor(Math.random() * words.size());
		String word = words.get(random);
		char[] chars = word.toCharArray();

		Letter[] letters = new Letter[Lingo.NR_OF_LETTERS];
		for (int i = 0; i < Lingo.NR_OF_LETTERS; i++) {
			letters[i] = new Letter(chars[i]);
		}

		Logger.getAnonymousLogger().log(Level.INFO, "The solution is: " + word);

		return letters;
	}

	/**
	 * @return a string of letters that are possible as next letter
	 */
	public String getPossibleNextLetters(String prefix) {
		Set<Character> hs = new TreeSet<Character>();
		
		for (int i = 0; i < words.size(); i++) {
			if(words.get(i).startsWith(prefix))
			{
				hs.add(words.get(i).charAt(prefix.length()));
			}
		}
		
		return hs.toString();
	}

}
